<template>
	<view class="container">
		<navBar color="#000" background="#e0dac9" :title="$t('sluetooth.search')"></navBar>
		<view class="web-cont">
			<!-- 雷达扫码开始 -->
			<radar :imageUrl="imageUrl"></radar>
			<view class="web-text">
				<view>{{$t('home.Bluetooth-prompt1')}}</view>
				<view>{{$t('home.Bluetooth-prompt2')}}</view>
			</view>
		</view>
		<view class="web-item">
			<view class="web-title flex">
				<view>{{$t('home.Searching-for-devices')}}</view>
				<uni-load-more class="icon-zq" :status="status" :content-text="contentText" :iconSize="18" />
			</view>
			<!-- <scroll-view scroll-y style="width:690rpx;height:500rpx">
				<block v-for="(item,index) in devicesList" :key="index">
					<view class="list-item" @click="Connect(item.deviceId)">
						<view style="display:flex;flex-direction:column;width:80%">
							<text style="font-size:medium;word-break:break-all">设备名称: {{item.name}}</text>
							<text style="font-size:x-small;color:gray;word-break:break-all">设备ID:
								{{item.deviceId}}</text>
							<text style="font-size:x-small;color:gray;word-break:break-all">信号强度RSSI:
								{{item.RSSI}}</text>
						</view>
						<image style="width:36px;height:36px" mode="aspectFit" src="/images/bluetooth.png"></image>
					</view>
				</block>
			</scroll-view> -->
			<view class="item-box">
				<view class="item" :class="[isActive == index ? 'active' : '']" v-for="(item, index) in devicesList"
					@click="Connect(item,index)" :key="index">{{item.localName}}</view>
			</view>
			<!-- <view class="item-box">
				<view class="item" :class="[isActive == index ? 'active' : '']" v-for="(item, index) in 5"
					@click="Connect(item.deviceId,index)" :key="index">jueli1</view>
			</view> -->
		</view>
		<!-- <button type="primary" class="button" :loading="searching"
			@click="Search">{{searching?"搜索中...":"搜索蓝牙设备"}}</button> -->
	</view>

</template>

<script>
	import radar from '@/componets/radar.vue';
	import xBlufi from '@/utils/blufi/xBlufi.js'
	export default {
		components: {
			radar
		},
		data() {
			return {
				isActive: null,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: this.$t('loading'),
					contentnomore: '没有更多'
				},
				status: 'loading', //加载中样式
				imageUrl: 'https://be.coucoq.cn/jueli/static/2025/01/07/f82c3b7d-bd9e-4a43-a3b8-a205d7493622.png',
				devicesList: [],
				searching: false,
			}
		},
		onLoad() {
			this.devicesList = []
			console.log("xBlufi", xBlufi)
			// xBlufi.initXBlufi(1);
			xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
			this.Search()
		},
		onReady() {

		},
		onUnload: function() {
			this.devicesList = []
			xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
		},
		methods: {
			Search: function() {
				console.log(this.searching, 'this.data.searching')
				if (this.searching) {
					console.log(this.searching, 'true')
					xBlufi.notifyStartDiscoverBle({
						'isStart': false
					})
				} else {
					console.log(this.searching, 'false')
					xBlufi.notifyStartDiscoverBle({
						'isStart': true
					})
				}
			},
			Connect: function(e, index) {
				this.isActive = index;
				//停止搜索
				xBlufi.notifyStartDiscoverBle({
					'isStart': false
				})
				// for (var i = 0; i < this.devicesList.length; i++) {
				// if (e === this.devicesList[i].deviceId) {
				// let name = this.devicesList[i].name
				console.log('点击了，蓝牙准备连接的e:' + e)
				console.log('点击了，蓝牙准备连接的deviceId:' + e.deviceId)
				xBlufi.notifyConnectBle({
					isStart: true,
					deviceId: e.deviceId,
					name: e.name
				});
				uni.showLoading({
					title: `${this.$t('connecting.bluetooth.devices')}...`,
				})
				// }
				// }
			},
			funListenDeviceMsgEvent(options) {
				console.log(options, 'options')
				switch (options.type) {
					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
						if (options.result) {
							let arr = []
							options.data.forEach(res => {
								if (res.name) {
									arr.push(res)
								}
							})
							this.devicesList = arr
							console.log(this.devicesList, '蓝牙')
						}
						// _this.setData({
						// 	devicesList: options.data
						// });

						break;

					case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
						console.log("连接回调：" + JSON.stringify(options))
						if (options.result) {
							uni.hideLoading()
							uni.showToast({
								title: this.$t('connection.successful'),
								icon: 'none'
							})
							uni.navigateTo({
								url: '/pagesA/device/wifiNet/device?deviceId=' + options.data.deviceId + '&name=' +
									options.data.name,
							});

						} else {
							uni.hideLoading()
							uni.showModal({
								title: this.$t('Tips'),
								content: this.$t('Bluetooth.failed'),
								showCancel: false
							});
						}
						break;

					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
						if (!options.result) {
							console.log("蓝牙未开启 fail =》", options)
							uni.showToast({
								title: '蓝牙未开启',
								icon: 'none'
							})
						} else {
							//蓝牙搜索开始
							// _this.setData({
							// 	searching: true
							// });
							this.searching = true
						}
						break;

					case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
						if (options.result) {
							//蓝牙停止搜索ok
							console.log('蓝牙停止搜索ok')
						} else {
							//蓝牙停止搜索失败
							console.log('蓝牙停止搜索失败')
						}
						// _this.setData({
						// 	searching: false
						// });
						this.searching = false
						break;
					case xBlufi.XBLUFI_TYPE.TYPE_CLOSE_CONNECTED:
						if (options.result) {
							//蓝牙停止搜索ok
							this.devicesList = []
							this.Search()
						} else {
							//蓝牙停止搜索失败
							console.log('蓝牙停止搜索失败')
						}
						// _this.setData({
						// 	searching: false
						// });
						this.searching = false
						break;


				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.web-cont {
			background: #E0DAC9;

			.web-text {
				line-height: 49rpx;
				font-size: 30rpx;
				text-align: center;
				margin-top: 80rpx;
			}
		}

		.web-item {
			background-image: linear-gradient(to bottom, #E0DAC9, rgba(243, 238, 230, 0.1));
			margin: auto;
			padding: 50rpx 0;
			font-size: 30rpx;

			.scroll {
				padding: 30rpx;
				background-color: #fff;
				margin: 30rpx;
			}

			.web-title {
				width: 90%;
				margin: 20rpx auto;

				.icon-zq {
					margin-left: 10rpx;
				}
			}

			.item-box {
				width: 90%;
				margin: auto;
				background: #fff;
				border-radius: 15rpx;
				overflow: hidden;

				.item {
					line-height: 100rpx;
					border-bottom: 1rpx solid #f5f5f5;
					padding: 0 40rpx;
					color: #666666;
				}
			}
		}

	}

	.list-item {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		padding: 10px 0 10px 0;
		box-sizing: border-box;
		border: 1px solid #000;
		border-style: none none solid none;
		border-bottom-color: lightgray;
	}

	.list-item:last-child {
		border-style: none;
	}

	.button {
		position: fixed;
		width: 690rpx;
		bottom: 30rpx;
	}

	.active {
		background: #f5f5f5;
	}
</style>